package com.example.hangman_in_android.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.example.hangman_in_android.R;

public class MainActivity extends Activity implements OnClickListener {
	
	// Buttons
	private Button buttonOnePlayer;
	private Button buttonTwoPlayer;
	private Button buttonOption;
	private Button buttonPlay;

	// Menu
	private Menu optionMenu;

	// menu Items
	private MenuItem itemSound;
	private MenuItem itemAbout;

	// radioButton on dialog box for sound
	private RadioGroup RGSound;
	private RadioButton rBSoundOn;
	private RadioButton rBSoundOff;

	// Media Player
	private static MediaPlayer mediaPlayer = null;

	private Dialog soundDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		allComponentInit();

		// // buttons
		// buttonOnePlayer = (Button) findViewById(R.id.buttonOnePlayer);
		// buttonOnePlayer.setOnClickListener(this);
		//
		// buttonTwoPlayer = (Button) findViewById(R.id.buttonTwoPlayer);
		// buttonTwoPlayer.setOnClickListener(this);
		//
		// buttonOption = (Button) findViewById(R.id.buttonOptions);
		// buttonOption.setOnClickListener(this);
		//
		// buttonPlay = (Button) findViewById(R.id.buttonPlay);
		// buttonPlay.setOnClickListener(this);

		// TODO radiobutton for sound should be implemented
		// TODO dont play sound on start but on resume HOW?

		// // radio buttons
		// RGSound = (RadioGroup) findViewById(R.id.RGSound);
		// DOES not WOrk
		// rBSoundOn = (RadioButton) findViewById(R.id.RBSoundOn);
		// rBSoundOff = (RadioButton) findViewById(R.id.RBSoundOff);
		// rBSoundOff.setOnCheckedChangeListener((OnCheckedChangeListener)
		// this);
		// rBSoundOn.setOnCheckedChangeListener((OnCheckedChangeListener) this);
		// rBSoundOn.setChecked(true);

		// // for long press of a button
		// registerForContextMenu(buttonOption);
		//
		// // Media player
		// mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.beat);

	}

	/**
	 * this is for users when pressing menu button
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// getMenuInflater().inflate(R.menu.option_menu, menu);
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.option_menu, menu);

		optionMenu = menu;

		// adding menu items;
		itemSound = menu.findItem(R.id.itemSound);
		itemAbout = menu.findItem(R.id.itemAbout);

		return true;
	}

	/**
	 * Context Menu
	 */
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.sound_context_menu, menu);

	}

	// TODO I get an ERROR when trying to do so on onStop
	@Override
	protected void onDestroy() {
		super.onDestroy();
		// Music Off
		if (mediaPlayer != null) {
			mediaPlayer.stop();
			mediaPlayer.release();
			mediaPlayer = null;
		}
	}

	@Override
	protected void onStop() {
		super.onStop();
		if ((mediaPlayer != null) && mediaPlayer.isPlaying()) {
			mediaPlayer.pause();
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		// Music pause
		// if ((mediaPlayer != null) && mediaPlayer.isPlaying()) {
		// mediaPlayer.pause();
		// }
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (mediaPlayer != null) {
			// mediaPlayer.start();
		}
	}

	/**
	 * buttons logic
	 */
	public void onClick(View v) {
		Intent intent;
		if (v == buttonOnePlayer) {
			intent = new Intent(this, OptionMenuActivity.class);
			startActivity(intent);

		} else if (v == buttonTwoPlayer) {
			String warning = "Not implemented yet";
			Toast toast = Toast.makeText(this, warning, 5000);
			Toast.makeText(this, warning, 5000);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();

		} else if (v == buttonOption) {
			System.out.println("Option Menu");

		} else if (v == buttonPlay) {
			// playing Sound
			if (mediaPlayer.isPlaying()) {
				mediaPlayer.pause();
			} else {
				mediaPlayer.start();
			}

		} else {
			System.out.println("ERROR");
		}

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.itemSound:
			soundDialog = new Dialog(this);
			soundDialog.setContentView(R.layout.sound_dialog);
			soundDialog.setTitle("Sound Options");
			soundDialog.show();

			break;

		case R.id.itemAbout:
			startActivity(new Intent(Intent.ACTION_VIEW,
					Uri.parse("http://en.wikipedia.org/wiki/Hangman_(game)")));
			break;

		default:
			startActivity(new Intent(Intent.ACTION_VIEW,
					Uri.parse("http://google.com")));
			break;
		}

		return super.onOptionsItemSelected(item);
	}

	private void allComponentInit() {
		// buttons
		buttonOnePlayer = (Button) findViewById(R.id.buttonOnePlayer);
		buttonOnePlayer.setOnClickListener(this);

		buttonTwoPlayer = (Button) findViewById(R.id.buttonTwoPlayer);
		buttonTwoPlayer.setOnClickListener(this);

		buttonOption = (Button) findViewById(R.id.buttonOptions);
		buttonOption.setOnClickListener(this);

		buttonPlay = (Button) findViewById(R.id.buttonPlay);
		buttonPlay.setOnClickListener(this);

		// for long press of a button
		registerForContextMenu(buttonOption);

		// Media player
		mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.beat);

	}

	/**
	 * for radio buttons
	 */
	// public void onCheckedChanged(RadioGroup group, int checkedId) {
	// switch (checkedId) {
	// case R.id.RBSoundOn:
	// System.out.println("SOUUUN ONN");
	//
	// break;
	//
	// case R.id.RBSoundOff:
	// System.out.println("SOUND OFF");
	// break;
	//
	// default:
	// System.out.println("MAAAAAAAAAAAAAAAAN");
	// break;
	// }
	//
	// }
}
